<?php
class site_k_structure extends ss_konstrukt_masterComponent {
	protected $templates;
	protected $structureMapper;
	protected $entryMapper;
	protected $strucure;
	protected $node;
	
	public function __construct(ss_core_settings $ss_settings, 
			site_templatefactory $templates, 
			site_dao_structure_mapper $structureMapper, 
			site_dao_entry_mapper $entryMapper,
			ss_core_injectorInterface $injector){
		$this->structureMapper=$structureMapper;
		$this->entryMapper=$entryMapper;
		$this->structure=$structureMapper->master();
    	$this->templates = $templates;
		$this->settings=$ss_settings;
		$this->injector=$injector;
	}
	
	public function dispatch(){
		$this->node=$this->structure->getNodeFromSlug($this->name());
		$this->structureMapper->meta($this->node);
		$this->document->setTitle("Structure ".$this->node->headline());
		$this->document->addNavigation($this->node->level(),$this->node);
		$this->document->addHeader($this->node->header(),$this->node->header_method(),$this->node->header_recurse());
		$this->entries=$this->entryMapper->getEntryByStructure($this->node->primary());
		return parent::dispatch();
	}
  
	protected function map($name){
		if(is_string($class=$this->structure->classForSlug($name))) return $class;
		if($this->entry=$this->entryMapper->getEntryBySlug($name)->clean())return 'site_k_entry';
	}
	
	public function entry(){
		return $this->entry;
	}
	
	protected function renderHtml() {
		$this->structure->getMenu($this->name());
		$entries=array();
		foreach($this->entries as $obj){
			$entries[]=$obj->master();
		}
		ss_error::log($entries);
    	$smarty = $this->templates->create();
	    $smarty->assign('content', $entries);
	    $smarty->assign('context', $this);
		$smarty->register_object('ss_path', $this->settings->server->appath);
    	return new k_HtmlResponse($smarty->fetch("master.tpl"));
	}
}
?>